package com.api.phm.domain.vo;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.api.common.utils.VER.AllowValue.AllowValue;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AlarmRuleQueryListVo {
    private Long ruleId;
    private String ruleName;
    //    @Enum(clazz = RuleTypeEnum.class, method = "getRuleType", allowNull = true, message = "规则类型只能输入规定类型")
    private String ruleType;
    private JSONObject deviceMap;
    @AllowValue(value = "PLC,STC", message = "规则模型不符合。")
    private String ruleMold;
    private String queryTab;
    private String appLang;
    private Integer pageNum;
    private Integer pageSize;
    /**
     * 更新人
     */
    private String updateBy;
    /**
     * 创建人
     */
    private String createBy;

    public void initQueryTab() {
        if (StringUtils.equals(ruleMold, "PLC")) {
            PlcRuleModel plcRuleModel = new PlcRuleModel(deviceMap);
            this.queryTab = plcRuleModel.newJsonTabSql();
        }
    }

    public void initControl(String username, String appLang) {
        this.appLang = appLang;
        this.updateBy = username;
        this.createBy = username;
    }
}

@Getter
@Setter
class PlcRuleModel {
    private Long plcId;
    private Long robotId;
    private Long elementId;
    private String datCol;

    public PlcRuleModel(JSONObject jo) {
        if (jo.containsKey("plcId")) plcId = jo.getLong("plcId");
        if (jo.containsKey("robotId")) robotId = jo.getLong("robotId");
        if (jo.containsKey("elementId")) elementId = jo.getLong("elementId");
        if (jo.containsKey("datCol")) datCol = jo.getString("datCol");
    }

    public String newJsonTabSql() {
        String joTable = "(SELECT DISTINCT tra.* FROM t_rule_alarm tra,JSON_TABLE(tra.device_map, '$.*'"
                + "COLUMNS ( key_name VARCHAR(255) PATH '$.key_name',\n";
        String whereSql = " where 1=1 ";
        if (ObjectUtils.isNotNull(this.plcId)) {
            joTable += " plcId BIGINT(19) PATH '$.plcId',\n";
            whereSql += " and jsonTable.plcId = " + this.plcId + "\n";
        }
        if (ObjectUtils.isNotNull(this.robotId)) {
            joTable += " robotId BIGINT(19) PATH '$.robotId',\n";
            whereSql += " and jsonTable.robotId = " + this.robotId + "\n";
        }
        if (ObjectUtils.isNotNull(this.elementId)) {
            joTable += " elementId BIGINT(19) PATH '$.elementId',\n";
            whereSql += " and jsonTable.elementId = " + this.elementId + "\n";
        }
        if (ObjectUtils.isNotNull(this.datCol)) {
            joTable += " datCol VARCHAR(20) PATH '$.datCol',\n";
            whereSql += " and jsonTable.datCol = " + this.datCol + "\n";
        }
        joTable += ")) AS jsonTable\n " + whereSql + ")";

        return joTable;
    }
}

//@Getter
//@Setter
//class StcRuleModel {
//    private Long stcId;
//    private String checkItem;
//}